package com.example.emojibase.utils;

import static android.content.ContentValues.TAG;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class CommonUtils {

    //将图片转化为位图
    public static void bitmabToBytes(Context context, SQLiteDatabase db) {
        InputStream is = null;
        byte[] data;
        try {
            // 打开 assets 文件夹中的文件
            is = context.getResources().getAssets().open("DefaultEmojis.bin");
            // 获取文件大小
            int length = is.available();
            // 创建 byte 数组
            data = new byte[length];
            // 读取数据到 byte 数组
            is.read(data);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭 InputStream
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        int offset = 0;
        List<byte[]>dataList=new ArrayList<>();
        while (offset < data.length) {
            int length = byteArrayToInt(data, offset);
            offset += 4; // 跳过长度信息
            // 读取数组本身
            byte[] datas = new byte[length];
            System.arraycopy(data, offset, datas, 0, length);
            dataList.add(datas);
            offset += length; // 移动到下一个数组的起始位置
            //Log.i(TAG, "bitmabToBytes: "+j);
        }
        int i=0;
        String name="Default";
        for (byte[] datas: dataList){
            ContentValues cv = new ContentValues();
            cv.put("name", name+i);
            i++;
            cv.put("avatar", datas);//图片转为二进制
            db.insert("emoji", null, cv);
        }
    }
    private static int byteArrayToInt(byte[] bytes, int offset) {
        return ((bytes[offset] & 0xFF) << 24) |
                ((bytes[offset + 1] & 0xFF) << 16) |
                ((bytes[offset + 2] & 0xFF) << 8) |
                (bytes[offset + 3] & 0xFF);
    }

    public static void bitmapToFile(byte[] img, String targetPath, String name){
        File file = new File(targetPath);
        System.out.println(file.getAbsolutePath());
        if (!file.exists()) {
            new File(targetPath).mkdir();
        }
        FileOutputStream fos = null;
        Log.i(TAG, "readBin2Image: "+targetPath);
        try {
            file=new File(targetPath+name+".png");
            fos = new FileOutputStream(file);
            fos.write(img);
            fos.flush();
        } catch (Exception e) {
            Log.e(TAG, "readBin2Image: ", e);
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    Log.e(TAG, "readBin2Image: ", e);
                }
            }
        }
    }

}
